﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace VSM.Parts
{
    public partial class Available2PromisePopup : System.Web.UI.Page
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session[Utils.AppConstants.ID_User] == null
                || Session[Utils.AppConstants.Password] == null
                || Session[Utils.AppConstants.ID_Dealer] == null)
            {
                Response.Redirect("Login.aspx");
            }
            if (!IsPostBack)
            {
                hdnItemCode.Value = Request.QueryString["ItemCode"];
                hdnOnHand.Value = Request.QueryString["OnHand"];
                LoadAvailable2Promise();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnAccept_Click(object sender, EventArgs e)
        {
            try
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "OK", "Main.okDialogClick('CloseForm');", true);
            }
            catch (Exception ex)
            {

            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void grdAva_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes.Add("onmouseover", "MouseEvents(this, event)");
                e.Row.Attributes.Add("onmouseout", "MouseEvents(this, event)");
                e.Row.Attributes["style"] = "cursor:pointer";
            }
        }

        private void LoadAvailable2Promise()
        {
            try
            {
                string dbType = Session[Utils.AppConstants.ID_Dealer].ToString() == "VSM" ? Utils.AppConstants.VSM : Utils.AppConstants.DEALER;
                GeneralFunctions GF = new GeneralFunctions(dbType, Session[Utils.AppConstants.ID_Dealer].ToString());

                DataTable dtAvailable = GF.GetMasterListBySqlCommand(Utils.AppConstants.VSM, "SELECT * FROM vw_Parts_Available2Promise WHERE ItemCode = '" + hdnItemCode.Value + "' ORDER BY DeliveryDate");

                DataTable dt = new DataTable("vw_Parts_Available2Promise");
                dt.Columns.Add("DeliveryDate");
                dt.Columns.Add("SONo");
                dt.Columns.Add("IsCommited");
                dt.Columns.Add("PONo");
                dt.Columns.Add("OnOrder");
                dt.Columns.Add("WhsName");
                dt.Columns.Add("AvailableQty");

                int liOnhand = int.Parse(string.IsNullOrEmpty(hdnOnHand.Value) ? "0" : hdnOnHand.Value);

                foreach (DataRow dr in dtAvailable.Rows)
                {
                    DataRow row = dt.NewRow();
                    row["DeliveryDate"] = Convert.ToDateTime(dr["DeliveryDate"].ToString()).ToString("dd/MM/yyyy");
                    row["SONo"] = dr["SONo"].ToString().Equals("0") ? "" : dr["SONo"].ToString();

                    int IsCommited = int.Parse(string.IsNullOrEmpty(dr["IsCommited"].ToString()) ? "0" : dr["IsCommited"].ToString());
                    row["IsCommited"] = IsCommited == 0 ? "" : IsCommited.ToString();
                    row["PONo"] = dr["PONo"].ToString().Equals("0") ? "" : dr["PONo"].ToString();

                    int OnOrder = int.Parse(string.IsNullOrEmpty(dr["OnOrder"].ToString()) ? "0" : dr["OnOrder"].ToString());
                    row["OnOrder"] = OnOrder == 0 ? "" : OnOrder.ToString();
                    row["WhsName"] = dr["WhsName"];

                    liOnhand = liOnhand - IsCommited + OnOrder;
                    row["AvailableQty"] = liOnhand;

                    dt.Rows.Add(row);
                }

                if (dt != null && dt.Rows.Count > 0)
                {
                    BindBP(dt);
                }
                else
                {
                    BindBP(null);
                }
            }
            catch (Exception ex)
            {

            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="tb"></param>
        private void BindBP(DataTable tb)
        {
            Session["AvailableTable"] = tb;
            this.grdAva.DataSource = tb;
            this.grdAva.DataBind();
        }
    }
}
